/**
 * Copyright (C)  Gadglet .
 *
 * This file is part of Gadglet
 *
 * Gadglet is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Gadglet is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Gadglet. If not, see <http://www.gnu.org/licenses/>.
 */

package com.gadglet.client.gwt.google;


import com.gadglet.client.gwt.ui.DebugDialogBox;
import com.google.gwt.http.client.URL;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;

public class DocsFolderItem {
	private final String parentIdentifier = "http://schemas.google.com/docs/2007#parent";
	
	private final String  docBaseUrl = "https://docs.google.com/Doc?docid=";
	private final String  spreadsheetsBaseUrl = "https://spreadsheets.google.com/ccc?key=";
	private final String  otherBaseUrl ="https://docs.google.com/leaf?id=";
	private final String  presentationBaseUrl ="https://docs.google.com/present/edit?id=";
	
	private JSONObject item;
	
	private DebugDialogBox errorNotifier  = DebugDialogBox.getErrorNotifier();
	public DocsFolderItem (JSONObject folder)
	{
		this.item = folder;
	}
	
	public boolean hasParent(){
		boolean hasParent  = false;
		JSONArray link = null;
	
		try {
			link = item.get("link").isArray();
			
			for (int count = 0; count<link.size(); count++){
				if(link.get(count).isObject().get("rel").isString().stringValue().equals(parentIdentifier))
					hasParent =true;
			}
			
		} catch (Exception e) {
			errorNotifier.showError(5, e.getMessage());
		}
		
		return hasParent;
		
	}
	public String getParentID()
	{
		JSONArray link = null;
		String parent = "";
		
		try {
			link = item.get("link").isArray();
			
			for (int count = 0; count<link.size(); count++){
				if(link.get(count).isObject().get("rel").isString().stringValue().equals(parentIdentifier))
					parent =  link.get(count).isObject().get("href").isString().stringValue();
			}
			
		} catch (Exception e) {
			errorNotifier.showError(5, e.getMessage());
		}
		return parent;
		
	}
	
	public String getTitle()
	{
		String title = "";
		try {
			title =  item.get("title").isObject().get("$t").isString().stringValue();
		} catch (Exception e) {
			errorNotifier.showError(1, "no folder title ");
		}
		return title;
	}
	
	
	public String getID(){
		String id = "";
		try {
			id =  item.get("id").isObject().get("$t").isString().stringValue();
		} catch (Exception e) {
			errorNotifier.showError(1, "no folder id ");
		}
		return id;
	}
	
	public String getPublished(){
		String published = "";
		try {
			published =  item.get("published").isObject().get("$t").isString().stringValue();
		} catch (Exception e) {
			errorNotifier.showError(1, "no published date for folder  ");
		}
		return published;
	}

	public String getUpdated(){
		String updated = "";
		try {
			updated =  item.get("updated").isObject().get("$t").isString().stringValue();
		} catch (Exception e) {
			errorNotifier.showError(1, "no updated date for folder  ");
		}
		return updated;
	}
	
	public String getDocURL()
	{
		String href = "";
		JSONArray link = null;
		
		try {
			link = item.get("link").isArray();
			
			for (int count = 0; count<link.size(); count++){
				if(link.get(count).isObject().get("rel").isString().stringValue().equals("alternate"))
					href = link.get(count).isObject().get("href").isString().stringValue();
			}
			
		} catch (Exception e) {
			errorNotifier.showError(5, e.getMessage());
		}
		
		return URL.decode(href);
	}
	
	public String getDocType(){
		String docType = "";
		
	    docType = getResourceId().substring(0, getResourceId().indexOf(":"));	
		
	    return docType;
	}
	
	public String getResourceId(){
		String resourceId = "";
		try {
			resourceId =  item.get("gd$resourceId").isObject().get("$t").isString().stringValue();
		} catch (Exception e) {
			errorNotifier.showError(1, "no folder resourceId ");
		}
		return resourceId;
	}
	
	
	
	
	
}
